package com.youku.plugins.logo.view
{
   import flash.display.Sprite;
   import flash.display.DisplayObject;
   import com.youku.baseUI.BaseUILoader;
   import flash.utils.Timer;
   import com.youku.utils.AssetUtil;
   import flash.events.TimerEvent;
   import flash.events.Event;
   import flash.net.URLRequest;
   import flash.system.LoaderContext;
   import com.youku.interfaces.IverifyUserData;
   import flash.events.MouseEvent;
   import com.youku.utils.OpenWindow;
   
   public class Logo extends Sprite
   {
      
      public function Logo() {
         super();
         this.init();
      }
      
      private var _youkuWatermark:DisplayObject;
      
      private var _userImg:BaseUILoader;
      
      private var _watermark:BaseUILoader;
      
      private var _homeUrl:String;
      
      private var _width:Number;
      
      private var _height:Number;
      
      private var _logoTimer:Timer;
      
      private var _loopTimer:Timer;
      
      private var _wmURL:String;
      
      private var _wmZ:Number;
      
      private var _wmX:Number;
      
      private var _wmY:Number;
      
      private var _wmT:Number;
      
      private var _waterMarkTimer:Timer;
      
      private var _hasUserImg:Boolean;
      
      private function init() : void {
         this._homeUrl = "";
         this._youkuWatermark = AssetUtil.getAsset("W_Youku_Logo");
         this._youkuWatermark.visible = false;
         this._youkuWatermark.alpha = 0.5;
         addChild(this._youkuWatermark);
         this._watermark = new BaseUILoader();
         this._watermark.useBitmap = true;
         this._watermark.scaleContent = false;
         this._watermark.maintainAspectRatio = true;
         this._watermark.showBackground = false;
         this._watermark.showBorder = false;
         this._userImg = new BaseUILoader();
         this._userImg.useBitmap = true;
         this._userImg.scaleContent = false;
         this._userImg.maintainAspectRatio = true;
         this._userImg.showBackground = false;
         this._userImg.showBorder = false;
         this._userImg.mouseEnabled = true;
         this._userImg.buttonMode = true;
         this._logoTimer = new Timer(10 * 1000,2);
         this._logoTimer.addEventListener(TimerEvent.TIMER,this.onLogoTimer);
         this._loopTimer = new Timer(3 * 60 * 1000);
         this._loopTimer.addEventListener(TimerEvent.TIMER,this.onLoop);
         this._waterMarkTimer = new Timer(1000);
         this._waterMarkTimer.addEventListener(TimerEvent.TIMER,this.onWaterMarkTimer);
         this._watermark.addEventListener(Event.COMPLETE,this.onWaterMarkLoaded);
         this._hasUserImg = false;
         this.updateLayout();
      }
      
      public function setSize(param1:Number, param2:Number) : void {
         this._width = param1;
         this._height = param2;
         this.updateLayout();
      }
      
      public function setWaterMark(param1:String, param2:int, param3:int, param4:int, param5:int) : void {
         this._wmURL = param1;
         this._wmZ = param2;
         this._wmX = param3;
         this._wmY = param4;
         this._wmT = param5;
         this._watermark.unload();
         this._watermark.load(new URLRequest(param1),new LoaderContext(false));
         this.addChild(this._watermark);
         this._waterMarkTimer.delay = this._wmT * 1000;
         this._waterMarkTimer.reset();
         this._waterMarkTimer.start();
      }
      
      public function tryToShowUserImg(param1:IverifyUserData) : void {
         this._homeUrl = param1.home_url;
         if(!this._hasUserImg)
         {
            this._userImg.addEventListener(Event.COMPLETE,this.onUserImgLoaded);
            this._userImg.load(new URLRequest(param1.avatar.middle),new LoaderContext(false));
         }
      }
      
      private function onWaterMarkLoaded(param1:Event) : void {
         this.updateLayout();
      }
      
      private function onUserImgLoaded(param1:Event) : void {
         this._userImg.removeEventListener(Event.COMPLETE,this.onUserImgLoaded);
         this._userImg.addEventListener(MouseEvent.CLICK,this.onUserImgClick);
         this._userImg.addEventListener(MouseEvent.MOUSE_OVER,this.onUserImgOver);
         this._userImg.addEventListener(MouseEvent.MOUSE_OUT,this.onUserImgOut);
         this._userImg.visible = false;
         this._userImg.alpha = 0.5;
         this._userImg.width = 50;
         this._userImg.height = 20;
         this._userImg.setSize(this._youkuWatermark.width,this._youkuWatermark.width);
         if((this._userImg) && (this.contains(this._userImg)))
         {
            this.removeChild(this._userImg);
         }
         this.addChild(this._userImg);
         this._hasUserImg = true;
         this.updateLayout();
         this.show();
      }
      
      private function onUserImgClick(param1:MouseEvent) : void {
         if((this._homeUrl) && !(this._homeUrl.indexOf("http://") == -1))
         {
            OpenWindow.open(new URLRequest(this._homeUrl));
         }
      }
      
      private function onUserImgOver(param1:MouseEvent) : void {
         this._userImg.alpha = 1;
      }
      
      private function onUserImgOut(param1:MouseEvent) : void {
         this._userImg.alpha = 0.5;
      }
      
      private function onWaterMarkTimer(param1:TimerEvent) : void {
         if(!(this._watermark == null) && (this.contains(this._watermark)))
         {
            this.removeChild(this._watermark);
         }
         this._waterMarkTimer.stop();
      }
      
      private function updateLayout() : void {
         if(this._wmZ == 0)
         {
            this._watermark.x = this._wmX;
            this._watermark.y = this._wmY;
         }
         else if(this._wmZ == 1)
         {
            this._watermark.x = this._wmX;
            this._watermark.y = this._height - this._watermark.height - this._wmY;
         }
         else if(this._wmZ == 2)
         {
            this._watermark.x = this._width - this._watermark.width - this._wmX;
            this._watermark.y = this._wmY;
         }
         else if(this._wmZ == 3)
         {
            this._watermark.x = this._width - this._watermark.width - this._wmX;
            this._watermark.y = this._height - this._watermark.height - this._wmY;
         }
         
         
         
         var _loc1_:Number = this._height / 460;
         this._youkuWatermark.scaleX = this._youkuWatermark.scaleY = _loc1_;
         this._youkuWatermark.x = this._width - int(this._youkuWatermark.width + 20 * _loc1_);
         this._youkuWatermark.y = int(20 * _loc1_);
         if(this._hasUserImg)
         {
            this._userImg.scaleX = this._userImg.scaleY = _loc1_;
            this._userImg.x = this._width - int(this._userImg.width + 20 * _loc1_);
            this._userImg.y = int(20 * _loc1_);
         }
      }
      
      public function show() : void {
         if(this._hasUserImg)
         {
            this._youkuWatermark.visible = true;
            this._userImg.visible = false;
            this._logoTimer.reset();
            this._logoTimer.start();
            this._loopTimer.reset();
            this._loopTimer.start();
         }
         else
         {
            this._youkuWatermark.visible = true;
         }
      }
      
      public function hide() : void {
         this._youkuWatermark.visible = false;
         if(this._hasUserImg)
         {
            this._userImg.visible = false;
         }
         this._logoTimer.stop();
         this._loopTimer.stop();
      }
      
      private function onLogoTimer(param1:TimerEvent) : void {
         var _loc2_:Timer = param1.target as Timer;
         if(_loc2_.currentCount == 1)
         {
            this._youkuWatermark.visible = false;
            this._userImg.visible = true;
         }
         else if(_loc2_.currentCount == 2)
         {
            this._youkuWatermark.visible = true;
            this._userImg.alpha = 0.5;
            this._userImg.visible = false;
         }
         
      }
      
      private function onLoop(param1:TimerEvent) : void {
         this.show();
      }
   }
}
